package com.cakeclient.clientsoap;


import com.cakeclient.clientsoap.Exception.SoapClientException;
import com.cakeclient.clientsoap.Exception.SoapComException;
import com.cakeclient.clientsoap.contract.*;

import java.util.*;

public class CakeClientSoapClient extends SoapClient {

    private StringMethod helloWorld = new StringMethod(this, "HelloWorld", null);
    private ArrayMethod<Store> getNearestStore = new ArrayMethod<Store>(this, "getNearestStore", null, Store.class);
    private ArrayMethod<Store> getNNearestStore = new ArrayMethod<Store>(this, "getNNearestStore", null, Store.class);
    private ArrayMethod<Store> getStoreList = new ArrayMethod<Store>(this, "getStoreList", null, Store.class);
    private ArrayMethod<Store> updateStoreList = new ArrayMethod<Store>(this, "updateStoreList", null, Store.class);
    private ObjectMethod<Catalog> getCatalog = new ObjectMethod<Catalog>(this, "getCatalog", null, Catalog.class);
    private ObjectMethod<Client> getClientFromId = new ObjectMethod<Client>(this, "getClientFromId", null, Client.class);
    private StringMethod CreateClient = new StringMethod(this, "CreateClient", null);
    private StringMethod UpdateClient = new StringMethod(this, "UpdateClient", null);
    private StringMethod CheckIdClient = new StringMethod(this, "CheckIdClient", null);
    private StringMethod CreateOrder = new StringMethod(this, "CreateOrder", null);
    private ArrayMethod<DeliveryType> GetDeliveryTypes = new ArrayMethod<DeliveryType>(this, "GetDeliveryTypes", null, DeliveryType.class);
    private StringMethod OrderState = new StringMethod(this, "OrderState", null);
    private ObjectMethod<Order> OrderDetails = new ObjectMethod<Order>(this, "OrderDetails", null, Order.class);
    private StringMethod addProductTypeToOrder = new StringMethod(this, "addProductTypeToOrder", null);


    public Method[] methodArray = new Method[]{
            helloWorld,
            getNearestStore,
            getNNearestStore,
            getStoreList,
            updateStoreList,
            getCatalog,
            getClientFromId,
            CreateClient,
            UpdateClient,
            CheckIdClient,
            CreateOrder,
            GetDeliveryTypes,
            OrderState,
            OrderDetails   ,
            addProductTypeToOrder
    };

    public List<Method> methodsList = new ArrayList<Method>(Arrays.asList(methodArray));


    @Override
    public String getNameSpace() {
        return "http://cakeathome.org/client/";
    }

    @Override
    public String getURI() {
        return "http://vincentcotro.fr/client/ServiceClient.asmx";
    }

    @Override
    public boolean isDotNet() {
        return true;
    }

    public String HelloWorld() throws SoapComException, SoapClientException {
        return helloWorld.getResult();
    }

    public List<Store> getNearestStore(GPSLocation position) throws SoapComException, SoapClientException {
        getNearestStore.setParameter("position", position);
        return getNearestStore.getResult();
    }

    public List<Store> getNNearestStore(GPSLocation position, int number) throws SoapComException, SoapClientException {
        HashMap<String, Object> parameters = new HashMap<String, Object>(2);
        parameters.put("position", position);
        parameters.put("number", number);
        getNNearestStore.setParameters(parameters);
        return getNNearestStore.getResult();
    }

    public List<Store> getStoreList() throws SoapComException, SoapClientException {
        return getStoreList.getResult();
    }

    //TODO : Rajouter le paramètre (je sais pas si les arrays en param ca fonctionne)
    public List<Store> updateStoreList() throws SoapComException, SoapClientException {
        return updateStoreList.getResult();
    }

    public Catalog getCatalog(String idMag) throws SoapComException, SoapClientException {
        getCatalog.setParameter("idMag", idMag);
        return getCatalog.getResult();
    }

    public Client getClientFromID(String DeviceID) throws SoapComException, SoapClientException {
        getClientFromId.setParameter("DeviceID", DeviceID);
        return getClientFromId.getResult();
    }

    public Integer CreateClient(SimpleClient client) throws SoapComException, SoapClientException {
        CreateClient.setParameter("client",client);
        return Integer.parseInt(CreateClient.getResult());
    }

    public Boolean UpdateClient(Client client) throws SoapComException, SoapClientException {
        UpdateClient.setParameter("client",client);
        return Boolean.parseBoolean(UpdateClient.getResult());
    }

    public Boolean CheckIdClient(String idClient) throws SoapComException, SoapClientException {
        CheckIdClient.setParameter("idClient",idClient);
        return Boolean.parseBoolean(CheckIdClient.getResult());
    }
    
    public List<DeliveryType> GetDeliveryTypes() throws SoapComException, SoapClientException {
        return GetDeliveryTypes.getResult();
    }
    
    public Integer OrderState( Order idOrder) throws SoapComException, SoapClientException {
        OrderState.setParameter("idOrder", idOrder);
         return Integer.parseInt(OrderState.getResult());
    }

    public Order OrderDetails(Integer idOrder) throws SoapComException, SoapClientException {
        OrderDetails.setParameter("idOrder", idOrder);
        return OrderDetails.getResult();
    }

    public boolean addProductTypeToOrder(int idProductType, int idOrder) throws SoapComException, SoapClientException {
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("idProductType", idProductType);
        parameters.put("idOrder", idOrder);
        addProductTypeToOrder.setParameters(parameters);
        return Boolean.parseBoolean(addProductTypeToOrder.getResult());
    }

    public Integer CreateOrder(SimpleOrder order) throws SoapComException, SoapClientException {
        CreateOrder.setParameter("order",order);
        return Integer.parseInt(CreateOrder.getResult());
    }



}
